const $common = require('../../common/common-req.js');
const requestHandler = require('../../common/request-handler.js');
const { StringDecoder } = require('string_decoder');
const decoder = new StringDecoder('utf8');
var router = $common.express.Router();

/**
 * 添加页面
 * */
router.post('/', async (req, res) => {
    const id = $common.strLen();
    const createTime = new Date();
    const updateTime = new Date();
    let param = ['mate', 'pageName', 'author', 'pageInfo'];
    let vital = ['mate', 'pageName', 'author', 'pageInfo'];
    let expand = {id, createTime, updateTime};
    let insertSql = 'insert into pages(id, mate, pageName, pageInfo, author, createTime, updateTime) values(?, ?, ?, ?, ?, ?, ?)';
    let insertArr = ['id', 'mate', 'pageName', 'pageInfo', 'author', 'createTime', 'updateTime'];
    let moduleName = '基础页面';
    let isExitArr = ['mate'];
    let exitTableName = 'pages';
    requestHandler.postHandler(req, res, param, vital, expand, insertSql, insertArr, moduleName, isExitArr, exitTableName);
})

/**
 * 获取页面列表
 * */
router.get('/', (req, res) => {
    let param = ['pageNum', 'pageSize', 'userId'];
    let vital = ['pageNum', 'pageSize', 'userId'];
    let totalSql = 'select count(id) as count from pages where author=?';
    let expand = [];
    let getSql = 'select * from pages where author=? order by updateTime limit ?, ?';
    let getArr = ['userId'];
    let selectAttr = ['id', 'mate', 'pageName', 'pageInfo', 'author', 'createTime', 'updateTime'];
    let reName = ['id', 'mate', 'pageName', 'pageInfo', 'author', 'createTime', 'updateTime'];
    let moduleName = '基础页面';
    let checkBlob = true;
    let blobArr = ['pageInfo'];
    requestHandler.getHandler(req, res, param, vital, totalSql, expand, getSql, getArr, selectAttr, reName, moduleName, checkBlob, blobArr);
})

/**
 * 根据id获取组件
 */
router.get('/byId', (req, res) => {
    let param = $common.getQueryParam(req, 'query', ['id', 'userId']);
    let { id, userId } = param
    let sql = 'select * from pages where id=? and author=?';
    let arr = [id, userId];
    $common.db_mysql.select(sql, arr, result => {
        let selectAttr = ['id', 'mate', 'pageName', 'pageInfo', 'author', 'createTime', 'updateTime'];
        let resArr = $common.selectHandle(result, selectAttr);
        if (resArr.length > 0) {
            // blob转字符串
            if (result[0].pageInfo) {
                resArr[0].pageInfo = decoder.write(result[0].pageInfo);
            } else {
                resArr[0].pageInfo = ''
            }
            $common.resData.data.list = resArr[0];
            $common.resData.msg = '获取页面成功';
            res.send($common.resData);
        } else {
            res.send($common.setErrorData('获取页面失败'));
        }
    })
})

/**
 * 修改页面
 * */
router.put('/', (req, res) => {
    const updateTime = new Date();
    let param = ['id', 'mate', 'pageName', 'pageInfo'];
    let vital = ['id', 'mate', 'pageName', 'pageInfo'];
    let expand = { updateTime };
    let updateSql = 'update pages set mate=?, pageName=?, pageInfo=? where id=?';
    let updateArr = ['mate', 'pageName', 'pageInfo', 'id'];
    let moduleName = '基础页面';
    requestHandler.putHandler(req, res, param, vital, expand, updateSql, updateArr, moduleName);
})

module.exports = router;